var simpleTree = function(data,pid){
  //console.log(pid);
  var r = data[pid||0] || [],res = [];
  if (r.length) {
    for(var i=0,j=r.length;i<j;i++){
      if (r[i]['level'] > 0) {
        var pad = new Array(r[i]['level']*1);
        //r[i]['prefix'] = pad.join("|\u00A0\u00A0") + "|\u00A0\u00A0-"
        r[i]['prefix'] = pad.join("/\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0") + "|-"
        //r[i]['prefix'] = pad.join("/@@") + "|-"
      }
      res.push(r[i]);
      if(data.hasOwnProperty(r[i]['id'])){
        res = res.concat(simpleTree(data,r[i]['id']));
      }
    }
  }
  //console.log(res)
  return res;
}
var childTree  = function (data, pid) {
    var r = data[pid||0] || []
    if (r.length){
        for(var i=0,j=r.length;i<j;i++){

            var rs = childTree(data,r[i]["id"])
            if (rs.length > 0) {
                r[i]["children"] = r[i]["children"] || []
                r[i]["children"] = r[i]["children"].concat(rs)
            }
        }
        return r;
    }
    return []
}
var treestruct = function(data,pid,pidvalue,genType){
  var tree = {},res = [],pidvalue = pidvalue || 0;
  for(var i=0,j=data.length;i<j;i++){
    tree[data[i][pid]] = tree[data[i][pid]] || []
    tree[data[i][pid]].push(data[i]);
  }
  if(typeof genType == undefined) {
      return simpleTree(tree, pidvalue);
  }
  else if(genType == "children"){
      return childTree(tree,pidvalue)
  }
}
export default {
  make : treestruct
}
